import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:series_play/comm/base/base_state.dart';
import 'package:series_play/comm/dialog_helper.dart';
import 'package:series_play/comm/ext/context_ext.dart';
import 'package:series_play/generated/assets.dart';
import 'package:series_play/utils/local_tool.dart';
import 'package:series_play/widget/app_image.dart';

import '../../widget/app_text.dart';

class LanguagePage extends StatefulWidget {
  const LanguagePage({super.key});

  @override
  State<LanguagePage> createState() => _LanguagePageState();
}

class _LanguagePageState
    extends BaseControllerState<LanguagePage, _GetController> {
  late List<String> tabs;

  @override
  Widget buildPage(BuildContext context) {
    tabs = [
      context.tr.languageEnglish,
      context.tr.languageSimplifiedChinese,
      context.tr.languageTraditionalChinese,
      context.tr.languageFrancais,
    ];

    final children = <Widget>[];
    for (int i = 0; i < tabs.length; i++) {
      children.add(normalItem(i, () => confirmSwitchDialog(i)));
      children.add(12.verticalSpace);
    }

    return Padding(
      padding: const EdgeInsets.all(16).w,
      child: Column(children: children),
    );
  }

  @override
  bool showDefaultBackground() {
    return true;
  }

  @override
  String getTitle() {
    return context.tr.languageSelect;
  }

  Widget normalItem(int index, VoidCallback onTap) {
    return GestureDetector(
      onTap: onTap,
      behavior: HitTestBehavior.translucent,
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16).w,
        height: 44.w,
        decoration: BoxDecoration(
          color: const Color(0xFF292B34).withValues(alpha: 0.5),
          borderRadius: BorderRadius.circular(6).r,
        ),
        alignment: Alignment.center,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            AppText.title(tabs[index], color: Colors.white),
            Obx(() {
              return AppImage(
                index == mController.activeIndex.value
                    ? Assets.imagesIcCheckboxDoubleCircleActive
                    : Assets.imagesIcCheckboxDoubleCircleNormal,
                size: 18,
              );
            }),
          ],
        ),
      ),
    );
  }

  confirmSwitchDialog(int index){
    DialogHelper.showSimpleDialog(context , title: '是否更换语言?' , onConfirm: (){
      mController.changeIndex(index);
    });
  }

  @override
  _GetController createController() {
    return _GetController();
  }
}

class _GetController extends GetxController {
  var activeIndex = 0.obs;

  @override
  void onReady() {
    super.onReady();
    initLocal();
  }

  initLocal() async {
    final local = await LocalTool.currentLocal();
    activeIndex.value = LocalTool.locals.indexOf(local);
  }

  changeIndex(int index) {
    activeIndex.value = index;
    LocalTool.changeLanguage(index);
  }
}
